<template>
<div class="">
  <product-list
  :list="proList"
  title=""
  @tap="onNavTo"
  />
</div>
</template>

<script>
import LocalStorage from '../utils/local-storage';
import utils from '../utils/util';
import TokenCheck from '../utils/token-check';

import Goods from '../api/goods/goods';

import ProductList from './public/ProductList.vue';

export default {
  name: 'home',
  components: {
    ProductList
  },
  data() {
    return {
      proList: [
        // {
        //   id: 0,
        //   name: 'iPhone XS',
        //   imgSrc: '../../static/images/1.jpg'
        // },
        // {
        //   id: 1,
        //   name: 'iPhone XS Max',
        //   imgSrc: '../../static/images/1.jpg'
        // },
      ]
    }
  },

  mounted() {
    // const {token} = this.$route.query;
    try {
      const s = window.location.search;
      if (s.includes('token=')) {
        const i = s.indexOf('=');
        const token = s.substring(i+1);
        // console.log(token);
        token && LocalStorage.setToken('Bearer ' + token);
        TokenCheck.clearTokenQuery();
      }
    } catch (e) {
      console.log(e);
    }

    // this.initToken();
    this.init();
  },

  methods: {
    init() {
      (async () => {
        const list = await Goods.getGoodsClasses();
        this.proList = this._listAdaptor(list);
      })()
    },

    _listAdaptor(list) {
      return list.map(el => {
        return {
          id: el.id,
          name: el.name,
          imgSrc: el.icon
        }
      });
    },




    onNavTo(e) {
      const {id, name} = e.item;
      this.$router.push({path: '/list', query: {id, name}})
    },

  }
}
</script>

<style scoped>



</style>
